<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	#import {
		position: relative;
		top: 1.4px;
	}
    #dynamic-table img {
		cursor: pointer;
		max-height: 200px;
	}
	#cboxClose {
		text-indent: 0;
		font-size: 50px;
		font-weight: bold;
		margin-top: 10px;
		color: red;
	}
	/* .work-div {
		position: relative;
	}
	#work-calendar {
		width: 200px;
		padding-left: 30px;
	} */
	.fa-calendar{
		position: absolute;
		left: 8px;
		top: 4px;
	}
	.hour-input {
		width: 160px;
		padding-left: 28px !important;
	}
	.outer-time-picker {
		position:relative;
	}
	.btn-search-time {
		position: relative;
		top: 1px;
		right: 4px;
		outline:none;
		/* height:33px; */
		height:34px;
		width:44px;
		border:1px solid #669FC7;
		background-color:#669FC7;
		border:0;
	}
</style>
<div class="page-header">
	<h1>
		学生考勤数据

			<a href="<?=admin_url('data/student_sign_today')?>" class="btn btn-info a-custom">
				<i class="ace-icon fa fa-table"></i> 查看今日考勤数据
			</a>

			<!-- <a href="javascript:;" id="export_student_sign" target="_blank" class="btn btn-info a-custom">
				<i class="ace-icon fa fa-download"></i> 导出考勤数据
			</a> -->
			<a href="javascript:;" id="export_student_sign" target="" class="btn btn-info a-custom">
				<i class="ace-icon fa fa-download"></i> 导出考勤数据
			</a>
			<a href="javascript:;" id="export_student_sign_img" target="" class="btn btn-info a-custom">
				<i class="ace-icon fa fa-download"></i> 导出考勤图片
			</a>
	</h1>
</div>
<div class="col-xs-12 content-filter">
	<!-- 筛选 -->
	<div class="widget-box">
		<div class="widget-header">
			<h4 class="widget-title">内容筛选</h4>
			<div class="widget-toolbar">
				<a href="#" data-action="collapse">
					<i class="ace-icon fa fa-chevron-up"></i>
				</a>
				<a href="#" data-action="close">
					<i class="ace-icon fa fa-times"></i>
				</a>
			</div>
		</div>
		<div class="widget-body">
			<div class="widget-main">
				<div class="form-inline" id="data-search">
					<div class="form-inline keyword-search-div">
						<label class="keyword-search-label">名称&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
						<div class="keyword-search-box">
							<div class="div-search">
								<input style="float:left;height:30px;width:180px;border:1px solid #669FC7;padding:0 30px 0 14px;border-radius:10px 0 0 10px !important;-moz-border-radius:10px 0 0 10px;" type="text" name="student-name" placeholder="搜索学生名称..." autocomplete="off">
								<span id="empty-search" style="position:relative;right:64px;cursor:pointer;display:none;">×</span>
								<button id="btn-search" class="btn-search" style="outline:none;float:left;height:30px;width:44px;border:1px solid #669FC7;background-color:#669FC7;border:0;border-radius:0 10px 10px 0;-moz-border-radius:0 10px 10px 0;">
									<i class="ace-icon fa fa-search nav-search-icon white"></i>
								</button>
							</div>
						</div>
					</div>

					<!-- 管理员权限 -->
					<?php if($rmxUser['user_type'] == USER_TYPE_SCHOOL): ?>
						<div class="form-inline keyword-search-div" id="div-grade">
							<label class="keyword-search-label">年级&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
							<div class="keyword-search-box">
								<a href="javascript:;" class="keyword-search-keyword keyword-search-grade active" data-id="0">全部</a>
								<?php if(isset($gradeList)): ?>
									<?php foreach($gradeList as $grade): ?>
										<a href="javascript:;" class="keyword-search-keyword keyword-search-grade"  data-id="<?=$grade['id']?>"><?=$grade['grade_name']?></a>
									<?php endforeach; ?>
								<?php endif ?>
							</div>
						</div>
						<div class="form-inline keyword-search-div" id="div-normal-class">
							<label class="keyword-search-label">班级&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
							<div class="keyword-search-box">
								<a href="javascript:;" class="keyword-search-keyword keyword-search-normal-class active" data-id="0">全部</a>
							</div>
						</div>
						<!-- <div class="form-inline keyword-search-div" id="div-teaching-class">
							<label class="keyword-search-label">教学班&nbsp;&nbsp;&nbsp;&nbsp;</label>
							<div class="keyword-search-box">
								<a href="javascript:;" class="keyword-search-keyword keyword-search-teaching-class active" data-id="0">全部</a>
							</div>
						</div> -->
					<?php else: ?>	
					<!-- 教师权限 -->
					<div class="form-inline keyword-search-div" id="div-normal-class">
							<label class="keyword-search-label">班级&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
							<div class="keyword-search-box div-classes">
								<a href="javascript:;" class="keyword-search-keyword keyword-search-class active" data-id="0">全部</a>
								<?php foreach($rmxUser['classArr'] as $class): ?>
									<a href="javascript:;" class="keyword-search-keyword keyword-search-class" data-id="<?=$class['id']?>"><?=$class['name']?></a>
								<?php endforeach; ?>
							</div>
						</div>
                    <?php endif ?>
                    <!-- <div class="form-inline keyword-search-div">
                        <label class="keyword-search-label">签到类型</label>
                        <div class="keyword-search-box" id="div-type">
                            <a href="javascript:;" class="keyword-search-keyword active" data-id="0">全部</a>
                            <a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_TYPE_CARD?>">刷卡</a>
                            <a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_TYPE_FACE?>">人脸识别</a>
							<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_TYPE_TEMPERATURE?>">热成像测温</a>
							<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_TYPE_TEMPERATURE_HEAD?>">额温枪测温</a>
							<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_TYPE_TEMPERATURE_WRIST?>">腕温测温</a>

							<?php if($gateOpen == true): ?>
								<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_TYPE_GATE?>">闸机</a>
							<?php endif ?>
							<?php if($aioOpen == true): ?>
								<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_TYPE_AIO?>">测温一体机</a>	
							<?php endif ?>
                        </div>
					</div>
					<div class="form-inline keyword-search-div">
                        <label class="keyword-search-label">签到状态</label>
                        <div class="keyword-search-box" id="div-state">
                            <a href="javascript:;" class="keyword-search-keyword active" data-id="0">全部</a>
                            <a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_STATE_NORMAL?>">行政班考勤</a>
                            <a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_STATE_NORMAL_SUPPLEMENT?>">行政班补签</a>
							<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_STATE_TEACH?>">教学班考勤</a>
							<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_STATE_TEACH_SUPPLEMENT?>">教学班补签</a>
							<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_STATE_ELECTIVE?>">选修班考勤</a>
							<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_STATE_ELECTIVE_SUPPLEMENT?>">选修班补签</a>
							<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_STATE_EXAM?>">考试签到</a>
							<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_STATE_EXAM_SUPPLEMENT?>">考试补签</a>
							<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_STATE_LEAVE?>">离校考勤</a>
							
							<?php if($gateOpen == true): ?>
								<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_STATE_GATE_ENTRY?>">闸机入口考勤</a>
								<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_STATE_GATE_EXIT?>">闸机出口口考勤</a>
							<?php endif ?>
							<?php if($aioOpen == true): ?>
								<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_STATE_AIO?>">一体机考勤</a>
								<a href="javascript:;" class="keyword-search-keyword" data-id="<?=SIGN_STATE_AIO_SUPPLEMENT?>">一体机补签</a>
							<?php endif ?>
							
                        </div>
					</div> -->
					<?php if($temperatureOpen): ?>
						<div class="form-inline keyword-search-div">
							<label class="keyword-search-label">体温&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
							<div class="keyword-search-box" id="div-temperature">
								<a href="javascript:;" class="keyword-search-keyword active" data-value="">全部</a>
								<a href="javascript:;" class="keyword-search-keyword" data-value="normal">正常</a>
								<a href="javascript:;" class="keyword-search-keyword" data-value="abnormal">异常</a>
							</div>
						</div>
					<?php endif ?>	
					<div class="form-inline keyword-search-div">
                        <label class="keyword-search-label" style="padding-top:5px;">签到时间&nbsp;&nbsp;&nbsp;&nbsp;</label>
                        <div class="">
							<span class="outer-time-picker">
								<input class="hour-input hour-input-begin" type="text" name="begin_time" value="<?= date('Y-m-d', strtotime('-7 day')) . ' 00:00' ?>" />
								<i class="fa fa-calendar"></i>
							</span>

								&nbsp;&nbsp;-&nbsp;&nbsp;

							<span class="outer-time-picker">
								<input class="hour-input hour-input-end" type="text" name="end_time" value="<?= date('Y-m-d') . ' 23:59' ?>" />
								<i class="fa fa-calendar"></i>
							</span>

							<button class="btn-search-time">
								<i class="ace-icon fa fa-search nav-search-icon white"></i>
							</button>
                        </div>
                    </div>	
				</div>
			</div>
		</div>
	</div>
</div>
<div class="col-xs-12">
	<!-- 学校表格 -->
	<table id="dynamic-table" class="table table-striped table-bordered table-hover mt10">
		<thead>
			<tr>
				<th>学生名称</th>
                <th>班级名称</th>
                <th>课程科目</th>
                <th>签到类型</th>
				<th>签到状态</th>
				<th>签到图片</th>
				<?php if($temperatureOpen): ?>
					<th>体温</th>
				<?php endif ?>
                <th>签到时间</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script src="<?= $staticAdminUrl ?>assets/laydate/laydate.js"></script>
<!-- 使用新的datetimepicker.js和css，解决设置不了pickerPosition -->
<link rel="stylesheet" href="<?=$staticAdminUrl?>assets/css/bootstrap-datetimepicker-new.min.css" />
<script src="<?=$staticAdminUrl?>assets/js/bootstrap-datetimepicker-new.min.js"></script>

<script type="text/javascript">
	var schoolId = 0;
	// 班级数据
	var normalClassJson = <?= $gradeNormalClassList ?>;
	$(function(){
		var table = $('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('data/get_student_sign_list?begintime=' . date('Y-m-d', strtotime('-7 day')) . ' 00:00' . '&endtime=' . date('Y-m-d') . ' 23:59' )?>',
			"columns": [
				{"data": "realname"},
                {"data": "class_name"},
                {"data": "schedule_lesson"},
                {"data": "sign_type"},
				{"data": "state"},
                {"data": function(ret) {
                    if(ret.sign_img) {
						return "<a href=" + ret.sign_img + " target='_blank'>查看图片</a>";
					} else {
						return ""
					}
				}},
				<?php if($temperatureOpen): ?>
					{"data": function(ret) {
						if(ret.temperature) {
							if(ret.temperature == '未知') {
								return "<span style='font-weight:bold;color:#337ab7;'>未知</span>";
							}
							var temperature = ret.temperature + "℃";
							var str = "";
							if(ret.temperature > 37.2) {
								str += "<span class='red' style='font-weight:bold;'>" + temperature + "</span>";
							} else {
								str += "<span class='green' style='font-weight:bold;'>" + temperature + "</span>";
							}
							return str;
						} else {
							return '';
						}
					}},
				<?php endif ?>	
				{"data": function(ret) {
					if(ret.schedule_class_index == 0) {
						// 闸机考勤没有节数
						return ret.schedule_week_index + "<br>" + ret.createtime;
					} else {
						return ret.schedule_week_index + "<br>" + "第" + ret.schedule_class_index + "节课" + "<br>" + ret.createtime;
					}
                }}
			],
			"callback": function() {

			}
		});
		$.fn.datetimepicker.dates['zh'] = {
		　　days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六","星期日"],
		　　daysShort: ["日", "一", "二", "三", "四", "五", "六","日"],
		　　daysMin: ["日", "一", "二", "三", "四", "五", "六","日"],
		　　months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月","十二月"],
		　　monthsShort: ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"],
		　　meridiem: ["上午", "下午"],
		　　today: "今天"
		};
		// 初始化时间选择器
		$(".hour-input").datetimepicker({
			// format: 'YYYY-MM-DD HH:mm',
			format: 'yyyy-mm-dd hh:ii',
			language: 'zh-CN',
			todayHighlight: true,
			autoclose: true,
			minuteStep: 1
		});
		// 点击日期图标触发点击日期input
		$(".fa-calendar").click(function() {
			$(this).siblings(".hour-input").focus();
		});

		// 年级班级联动
		$("#data-search-grade").change(function(){
			var gradeId = $(this).val();
			var classSelect = $("#data-search-class");
			// var teachSelect = $("#data-search-teach");
			var getUrl = "<?=admin_url('school/get_class_list')?>/" + gradeId;
			ajax_get(getUrl, {hideLoading: true}, function(res){
				var optionHtml = "";
				classSelect.html('<option value="">全部班级</option>');
				// teachSelect.html('<option value="">全部班级</option>');

				for(var i in res.data) {
					var classData = res.data[i];
					if(classData.class_type == '<?=CLASSES_TYPE_NORMAL?>') classSelect.append('<option value="'+ classData.id +'">'+ classData.class_name +'</option>');
					// if(classData.class_type == '<?=CLASSES_TYPE_TEACHING?>') teachSelect.append('<option value="'+ classData.id +'">'+ classData.class_name +'</option>');
				}
			})
		})
		// 点击关键字
		$("body").on("click", ".keyword-search-keyword", function(){
			var id = $(this).data("id");
			// 高亮当前点击对象
			$(this).parent().find(".keyword-search-keyword").removeClass("active");
			$(this).addClass("active");
			// 如果是点击年级
			if($(this).hasClass("keyword-search-grade")) {
				// 先清空班级列表
				$(".keyword-search-normal-class:gt(0)").remove();
				$(".keyword-search-teaching-class:gt(0)").remove();
				// 获取班级列表
				var normalClassList = normalClassJson[id];
				// var teachingClassList = teachingClassJson[id];
				for(var i in normalClassList) $(".keyword-search-normal-class:first").parent().append('<a href="javascript:;" class="keyword-search-keyword keyword-search-normal-class" data-id="'+ normalClassList[i].id +'">'+ normalClassList[i].name +'</a>');
				// for(var i in teachingClassList) $(".keyword-search-teaching-class:first").parent().append('<a href="javascript:;" class="keyword-search-keyword keyword-search-teaching-class" data-id="'+ teachingClassList[i].id +'">'+ teachingClassList[i].name +'</a>');
				// 选中全部班级
				searchData(table);
			} else {
				searchData(table);
			}
		});
		// 敲回车就搜索
		$("[name=student-name]").keypress(function (e) {
                if (e.which == 13) {
                    $("#btn-search").trigger("click");
                }
		});
		// 清空搜索框
		$("[name=student-name]").keyup(function() {
			if($(this).val().length > 0) {
				$("#empty-search").show();
				$("#empty-search").click(function(){
					$("[name=student-name]").val('');
					$(this).hide();
                    // 重置搜索结果
					searchData(table);
				})
			} else {
				$("#empty-search").hide();
			}
		}); 
		// 搜索标题
		$("#btn-search").click(function() {
			searchData(table);
		});
		// 搜索时间
		$(".btn-search-time").click(function() {
			var beginTime = $(".hour-input-begin").val();
			var endTime = $(".hour-input-end").val();
			if(beginTime != '' && endTime != '' && beginTime > endTime) {
				tips_alert("开始时间不能小于结束时间", false, function() {
				});
				$(".hour-input-begin").val('');
				return;
			}
			searchData(table);
		});
		// 导出数据
		$("#export_student_sign").click(function() {
			var html = $("#dynamic-table_info").html();
            if(html == "表中数据为空") {
				tips_alert("当前暂无数据", false);
				return;
            } else {
                var arr = html.split("，");
                var temp = arr[1];
                var temp2 = temp.replace("共", "");
                var num = temp2.replace("条结果", "");
				if(num.indexOf(',') != -1) {
					tips_alert("当前数据量过大，不能大于10000条", false);
					return;
				}
				if(parseInt(num) > 10000) {
					tips_alert("当前数据量过大，不能大于10000条", false);
					return;
				}
            }       

            var studentName = $("[name=student-name]").val();
            var type =$("#div-type .active").data("id");
			var state =$("#div-state .active").data("id");
			var temperature = $("#div-temperature .active").data("value");
			var gradeId = $("#div-grade .active").data("id");
            var classId = $("#div-normal-class .active").data("id");
			var beginTime = $(".hour-input-begin").val();
			var endTime = $(".hour-input-end").val();
			if(beginTime > endTime) {
				beginTime = '';
				endTime = '';
			}
			$url = "<?=admin_url('data/export_student_sign?')?>";
			$queryStr = '';
			$queryArr = [];
			$str = '';
			if(studentName) {
				$str = "keyword=" + studentName;
				$queryArr.push($str);
			}
			if(gradeId) {
				$str = "gradeId=" + gradeId;
				$queryArr.push($str);
			}
			if(classId) {
				$str = "classId=" + classId;
				$queryArr.push($str);
            }
            if(type) {
				$str = "type=" + type;
				$queryArr.push($str);
            }
			if(state) {
				$str = "state=" + state;
				$queryArr.push($str);
			}
			if(temperature) {
				$str = "temperature=" + temperature;
				$queryArr.push($str); 
			}
			if(beginTime) {
				$str = "begintime=" + beginTime;
				$queryArr.push($str);
			}
			if(endTime) {
				$str = "endtime=" + endTime;
				$queryArr.push($str);
			}
			$queryStr = $queryArr.join("&");
			// console.log($queryStr);return;
			window.location.href  = $url + $queryStr;
		});
		
		$("#export_student_sign_img").click(function() {
			var html = $("#dynamic-table_info").html();
            if(html == "表中数据为空") {
				tips_alert("当前暂无数据", false);
				return;
            } else {
                var arr = html.split("，");
                var temp = arr[1];
                var temp2 = temp.replace("共", "");
                var num = temp2.replace("条结果", "");
				if(num.indexOf(',') != -1) {
					tips_alert("当前数据量过大，不能大于500条", false);
					return;
				}
				if(parseInt(num) > 500) {
					tips_alert("当前数据量过大，不能大于500条", false);
					return;
				}
            }       

            var studentName = $("[name=student-name]").val();
            var type =$("#div-type .active").data("id");
			var state =$("#div-state .active").data("id");
			var temperature = $("#div-temperature .active").data("value");
			var gradeId = $("#div-grade .active").data("id");
            var classId = $("#div-normal-class .active").data("id");
			var beginTime = $(".hour-input-begin").val();
			var endTime = $(".hour-input-end").val();
			if(beginTime > endTime) {
				beginTime = '';
				endTime = '';
			}
			$url = "<?=admin_url('data/export_student_sign_img?')?>";
			$queryStr = '';
			$queryArr = [];
			$str = '';
			if(studentName) {
				$str = "keyword=" + studentName;
				$queryArr.push($str);
			}
			if(gradeId) {
				$str = "gradeId=" + gradeId;
				$queryArr.push($str);
			}
			if(classId) {
				$str = "classId=" + classId;
				$queryArr.push($str);
            }
            if(type) {
				$str = "type=" + type;
				$queryArr.push($str);
            }
			if(state) {
				$str = "state=" + state;
				$queryArr.push($str);
			}
			if(temperature) {
				$str = "temperature=" + temperature;
				$queryArr.push($str); 
			}
			if(beginTime) {
				$str = "begintime=" + beginTime;
				$queryArr.push($str);
			}
			if(endTime) {
				$str = "endtime=" + endTime;
				$queryArr.push($str);
			}
			$queryStr = $queryArr.join("&");
			// console.log($queryStr);return;
			window.location.href  = $url + $queryStr;
		});
	})
	function searchData(table) {
		var studentName = $("[name=student-name]").val();
		var type =$("#div-type .active").data("id");
		var state =$("#div-state .active").data("id");
		var temperature = $("#div-temperature .active").data("value");
		var gradeId = $("#div-grade .active").data("id");
		var classId = $("#div-normal-class .active").data("id");
		var beginTime = $(".hour-input-begin").val();
		var endTime = $(".hour-input-end").val();
		if(beginTime != "" && endTime != "" && beginTime > endTime) {
			beginTime = '';
			endTime = '';
		}
		table.setAjaxData({
			'keyword': studentName,
			"type": type,
			"state": state,
			"temperature": temperature,
			"gradeId": gradeId,
			"classId": classId,
			"begintime": beginTime,
			"endtime": endTime
		});
	}
</script>